package com.sskj.qiangjin.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.StockBloomBean;
import com.sskj.qiangjin.presenter.RechargeH5Presenter;

import java.nio.file.FileSystemAlreadyExistsException;

import butterknife.BindView;

@Route(path = RConfig.APP_RECHATRGE_WEBVIEW)
public class RechargeH5Activity extends BaseActivity<RechargeH5Presenter> {
    private static final String TAG = "RechargeH5Activity";
    @BindView(R.id.webView)
    WebView webView;
    @Autowired
    String rechargeUrl;

    private KProgressHUD progress;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_recharge_h5;
    }

    @Override
    public RechargeH5Presenter getPresenter() {
        return new RechargeH5Presenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);
        setTitle("充值");
        init();

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                setLoadingView();
                Log.i(TAG, "onPageStarted: ");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.i(TAG, "onPageFinished: ");
                if (isDestroyed()) {
                    return;
                }
                if (progress != null && progress.isShowing()) {
                    progress.dismiss();
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "shouldOverrideUrlLoading: ");
                if (url.contains("www.myjuejin.com")) {
                    ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
                }
                if (url.contains("www.aiqiangjin.com")) {
                    ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
                    finish();
                }
                if (url.startsWith("weixin://wap/pay?")
                        || url.startsWith("http://weixin/wap/pay")
                        || url.contains("alipays://platformapi")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    /**
     * 设置加载页面
     */
    private void setLoadingView() {
        progress = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("加载中")
                .setCancellable(true)
                .show();
    }

    @SuppressLint("SetJavaScriptEnabled")
    public void init() {
        String url = rechargeUrl;
        webView.loadUrl(url);
        //覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为，使得网页可以在WebView中打开

        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        //声明WebSettings子类
        WebSettings webSettings = webView.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        // 若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
        // 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可

        //支持插件

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
        webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
        webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progress.isShowing()) {
            progress.dismiss();
        }

    }
}
